/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.foundation.rutils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.io.Serializable;

/**
 * Created by Peter.Li on 2015/10/28.
 */
public class RConfigurationItem implements Serializable {
    private static final long serialVersionUID = 2L;

    private final String host;
    private final int port;
    private final String username;
    private final String password;

    /**
     * Create a new configuration item for an Rserve process.
     *
     * @param host     The host/ip Rserve is running on
     * @param port     The TCP port Rserve is listening on
     * @param username Username to supply for the connection
     * @param password Password to supply for the connection
     */
    RConfigurationItem(final String host, final int port, final String username, final String password) {
        super();
        assert StringUtils.isNotBlank(host) : "Host must be provided!";
        this.host = host;
        this.password = password;
        this.port = port;
        this.username = username;
    }

    /**
     * Get the host/ip Rserve is running on.
     *
     * @return the host/ip Rserve is running on
     */
    public String getHost() {
        return host;
    }

    /**
     * Get the TCP port Rserve is listening on.
     *
     * @return port The TCP port Rserve is listening on
     */
    public int getPort() {
        return port;
    }

    /**
     * Get the username to supply for the connection.
     *
     * @return Username to supply for the connection.
     */
    public String getUsername() {
        return username;
    }

    /**
     * Get the password to supply for the connection.
     *
     * @return Password to supply for the connection.
     */
    public String getPassword() {
        return password;
    }

    /**
     * Returns a string representation of the this configuration.
     *
     * @return a string representation of the object.
     */
    @Override
    public String toString() {
        return new ToStringBuilder(this).append("host", host).append("port", port).toString();
    }

    /**
     * Indicates whether some other configuration is "equal to" this one.
     *
     * @param obj the reference object with which to compare.
     * @return true if this object is the same as the obj argument; false otherwise.
     */
    @Override
    public boolean equals(final Object obj) {
        if (!(obj instanceof RConfigurationItem)) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        final RConfigurationItem rhs = (RConfigurationItem) obj;
        return new EqualsBuilder().append(host, rhs.host).append(port, rhs.port).isEquals();
    }

    /**
     * Returns a hash code value for the object.
     *
     * @return a hash code value for this object.
     */
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(host).append(port).toHashCode();
    }
}
